package com.bsb.biz.service;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.bsb.base.common.util.StringUtils;
import com.bsb.base.mapper.SysRoleMapper;
import com.bsb.base.model.SysOrganization;
import com.bsb.base.model.SysRole;
import com.bsb.base.model.SysRoleDeptExample;
import com.bsb.base.model.SysRoleExample;
import com.bsb.base.model.SysRoleMenuExample;
import com.bsb.base.model.SysRoleMenuExample.Criteria;

@Service
public class SysRoleService{
	
	@Resource
	private SysRoleMapper sysRoleMapper;
	@Resource
	private SysResourceService sysResourceService;
	@Resource
	private SysOrganizationService sysOrganizationService;
	@Resource
	private SysRoleMenuService sysRoleMenuService;
	@Resource
	private SysRoleDeptService sysRoleDeptService;
	
	public void addSysRole(SysRole sysRole) {
		sysRoleMapper.insertSelective(sysRole);
	}
	
	public void delSysRoleById(Long id) {
		sysRoleMapper.deleteByPrimaryKey(id);
	}
	
	public void updateSysRole(SysRole sysRole) {
		sysRoleMapper.updateByPrimaryKeySelective(sysRole);
	}
	
	public void updateSysRoleByExample(SysRole sysRole,SysRoleExample sysRoleExample){
	    sysRoleMapper.updateByExampleSelective(sysRole,sysRoleExample);
	}
	
	public SysRole getSysRoleById(Long id) {
		return sysRoleMapper.selectByPrimaryKey(id);
	}
	
	public List<SysRole> getPageSysRole(SysRoleExample sysRoleExample) {
		return sysRoleMapper.selectPage(sysRoleExample);
	}
	
	public int countSysRole(SysRoleExample sysRoleExample) {
		return sysRoleMapper.countByExample(sysRoleExample);
	}
	
	public List<SysRole> selectByExample(SysRoleExample example){
	    return sysRoleMapper.selectByExample(example);
	}
	
	 public Set<String> findRoles(String... roleIds) {
	        Set<String> roles = new HashSet<String>();
	        for(String roleId : roleIds) {
	        	SysRole role = getSysRoleById(Long.valueOf(roleId));
	            if(role != null) {
	                roles.add(role.getRole());
	            }
	        }
	        return roles;
	    }

	public Set<String> findPermissions(String[] roleIds) {
		 Set<Long> resourceIds = new HashSet<Long>();
	        for(String roleId : roleIds) {
	        	SysRole role = getSysRoleById(Long.valueOf(roleId));
	            if(role != null) {
	            	String resource_ids = role.getResource_ids();
	            	String[] split = resource_ids.split(",");
	            	for (String string : split) {
	            		resourceIds.add(Long.valueOf(string));
					}
	            }
	        }
	        return sysResourceService.findPermissions(resourceIds);
	}
	
	public SysOrganization getDeptByDeptId(String deptId){
		if(StringUtils.isEmpty(deptId)){
			return null;
		}
		SysOrganization sysOrganizationById = sysOrganizationService.getSysOrganizationById(Long.valueOf(deptId));
		return sysOrganizationById;
	}
	/**
	 * 创建新角色  关联角色菜单（菜单权限） 关联角色部门（数据权限）
	 * @param sysRole
	 */
	public void addRoleRelate(SysRole sysRole) {
		String menu_ids = sysRole.getResource_ids();
		String data_dept_ids = sysRole.getData_dept_ids();
		Long id = sysRole.getId();
		
		List<String> menulist = Arrays.asList(StringUtils.split(menu_ids,","));
		sysRoleMenuService.insertBatchRoleMenu(id, menulist);
		
		List<String> deptlist = Arrays.asList(StringUtils.split(data_dept_ids,","));
		sysRoleDeptService.insertBatchRoleDept(id, deptlist);
	}

	public void updateRole(SysRole sysRole) {
		sysRoleMapper.updateByPrimaryKeySelective(sysRole);
		delRoleRelate(sysRole);
		addRoleRelate(sysRole);
	}
	
	public void delRoleRelate(SysRole sysRole){
		Long id = sysRole.getId();
		SysRoleMenuExample sysRoleMenuExample = new SysRoleMenuExample();
		Criteria criteria = sysRoleMenuExample.createCriteria();
		criteria.andRole_idEqualTo(id);
		sysRoleMenuService.delByExample(sysRoleMenuExample );
		
		SysRoleDeptExample example = new SysRoleDeptExample();
		com.bsb.base.model.SysRoleDeptExample.Criteria createCriteria = example.createCriteria();
		createCriteria.andRole_idEqualTo(id);
		sysRoleDeptService.delByExample(example );
	}

	public void delRole(SysRole sysRole){
		Long id = sysRole.getId();
		SysRoleExample example = new SysRoleExample();
		com.bsb.base.model.SysRoleExample.Criteria criteria = example.createCriteria();
		criteria.andIdEqualTo(id);
		sysRoleMapper.deleteByExample(example );
		delRoleRelate(sysRole);
	}

	public List<SysRole> getRolesByDeptid(Long organization_id) {
		
		SysRoleExample example = new SysRoleExample();
		com.bsb.base.model.SysRoleExample.Criteria criteria = example.createCriteria();
		criteria.andDept_idEqualTo(organization_id.toString());
		return sysRoleMapper.selectByExample(example );
	}

	public void addRole(SysRole sysRole) {
		sysRoleMapper.insertSelective(sysRole);
		addRoleRelate(sysRole);
	}
}
